<template>
  <div class="login">
    <el-form
      ref="loginRef"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
    >
      <h3 class="title">云 课 考 试 管 理</h3>

      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          size="large"
          auto-complete="off"
          placeholder="账号"
        >
          <template #prefix><svg-icon icon="user" /></template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          size="large"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter="handleLogin"
        >
          <template #prefix><svg-icon icon="password" /></template>
        </el-input>
      </el-form-item>
      <div class="operator">
        <!-- <el-checkbox v-model="loginForm.rememberMe">记住密码</el-checkbox> -->
        <el-text class="mx-1" type="primary">忘记密码</el-text>
      </div>
      <el-form-item style="width: 100%">
        <el-button
          size="large"
          style="width: 100%; background-color: #abd0bf"
          @click.prevent="handleLogin"
        >
          <span>登 录</span>
        </el-button>
      </el-form-item>
    </el-form>

    <div class="el-login-footer">
      <!-- <span>Copyright © 2013-2022 <a href="http://www.java1234.vip" target="_blank">java1234.vip</a> 版权所有.</span> -->
    </div>
  </div>
</template>

<script>
import { VueCookies } from 'vue-cookies';
export default {
  data() {
    return {
      loginForm: {
        username: null,
        password: null,
        rememberMe:null,
      },
      loginRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 16,
            message: "长度在 6 到 16 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    const user = JSON.parse(localStorage.getItem("user"));
    console.log(user);
    if (localStorage.getItem("user") && user) {
      this.loginForm.password = user.password;
      this.loginForm.username = user.username;
      this.loginForm.rememberMe = user.rememberMe;
    }
    console.log(this.loginForm);
  },
  methods: {
    handleLogin() {
      this.$refs.loginRef.validate(async (e) => {
        if (!e) return;
        // if (this.loginForm.rememberMe) {
        //   localStorage.setItem("user", JSON.stringify(this.loginForm));
        // }
        const res = await this.$http({
          url: "/management/login",
          method: "POST",
          params: {
            username: this.loginForm.username,
            password: this.loginForm.password,
          },
        });
        if(res.data.message == 'ok'){
          this.$message.success("登录成功")
          this.$cookies.set("token",res.data.data)
          this.$cookies.set("admin",1)
          this.$router.push('/admin')
          console.log(res);
        }else{
          this.$message.error(res.data.data)
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  min-width: 1000px;
  // display: flex;
  // justify-content: center;
  // align-items: center;
  height: 100vh;
  background-color: #abd0bf;
  // background-size: auto auto;
  // background-repeat: no-repeat;

  position: relative;
}
// .login_bg{
//   width: 40%;
//   height: 100vh;

// }
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}
.operator {
  width: 100%;
  font-size: 16px;
  display: flex;

  justify-content: space-between;
  align-items: center;
  .el-text {
    cursor: pointer;
  }
}
.login-form {
  // border:1px #abd0bf solid;

  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  box-shadow: 3px 3px 10px rgba(0, 0, 0, 0.5);
  // top:30%;
  //
  .el-input {
    height: 40px;
    input {
      display: inline-block;
      height: 40px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 0px;
  }
}
.login-form:hover {
  transition: box-shadow 1s;
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 40px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 40px;
  padding-left: 12px;
}
</style>